package com.ymtms.arrivalofgoods.aspect;

import com.ymtms.arrivalofgoods.constant.ArriveConstant;
import com.ymtms.arrivalofgoods.dao.*;
import com.ymtms.arrivalofgoods.pojo.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Aspect
@Component
public class ArriveAspect {

    @Autowired
    private GoodsbillReceoptMainMapper goodsbillReceoptMainMapper;
    @Autowired
    private GoodsbillMasterMapper goodsbillMasterMapper;
    @Autowired
    private DriverInformationMapper driverInformationMapper;
    @Autowired
    private CustomerReceiptInformationMapper customerReceiptInformationMapper;
    @Autowired
    private GoodsReceiptInformationMapper goodsReceiptInformationMapper;

    // @Around("execution(* *..SomeService.doOther(..))")
    //@Around("execution(* *..CheckGoodsCustomerService.modifyStatusByNum(..))")
    public void aroundArriveHistory(ProceedingJoinPoint joinPoint) {
        try {
            joinPoint.proceed();
            Object[] args = joinPoint.getArgs();
            Integer num = (Integer) args[0];
            insertReceiptHistory(num);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }


    public void insertReceiptHistory(Integer grmid) {

        GoodsbillReceoptMain goodsbillReceoptMain = goodsbillReceoptMainMapper.selectByPrimaryKey(grmid);
        GoodsbillMaster goodsbillMaster = goodsbillMasterMapper.selectByPrimaryKey(goodsbillReceoptMain.getGoodsBillpkid());
        DriverInformation driverInformation = driverInformationMapper.selectByPrimaryKey(goodsbillReceoptMain.getDriverPkid());

        //插入客户回执表
        CustomerReceiptInformation customerReceiptInformation = new CustomerReceiptInformation();
        customerReceiptInformation.setGoodsBillCode(goodsbillMaster.getGoodsBillCode());
        customerReceiptInformation.setCustomer(goodsbillMaster.getSendGoodsCustomer());
        customerReceiptInformation.setCheckGoodsRecord(ArriveConstant.checked);
        customerReceiptInformation.setReceiveGoodsPerson(goodsbillReceoptMain.getLinkmanMobile());
        customerReceiptInformation.setReceiveGoodsDate(new Date());
        customerReceiptInformation.setCarryBillEventPkid(0);
        customerReceiptInformationMapper.insertSelective(customerReceiptInformation);
        //插入货物回执表
        GoodsReceiptInformation goodsReceiptInformation = new GoodsReceiptInformation();
        goodsReceiptInformation.setGoodsRevertCode(goodsbillReceoptMain.getGoodsRevertBillCode());
        goodsReceiptInformation.setDriverName(driverInformation.getDriverName());
        goodsReceiptInformation.setCheckGoodsRecord(ArriveConstant.checked);
        goodsReceiptInformation.setReceiveGoodsPerson(goodsbillReceoptMain.getReceiveGoodsLinkman());
        goodsReceiptInformation.setReceiveGoodsDate(new Date());
        goodsReceiptInformationMapper.insertSelective(goodsReceiptInformation);

    }


}
